namespace hjc {
	export class AStarViewer {

		private $map: AStarMap;
		public static DFT_NAME = 'A星地图';
		public static DFT_FLAG_CLR = Color.ORANGE;			//默认标记地块颜色
		public static DFT_OBST_CLR = Color.DARK_GRAY;		//默认障碍地块颜色
		public static DFT_FLAG_ALF = 0.5;					//默认标记地块透明度
		public static DFT_OBST_ALF = 0.8;					//默认障碍地块透明度
		private $grids: egret.Shape;						//网格层
		private $blocksLayer: egret.DisplayObjectContainer;	//地块层
		private $pathLayer: egret.DisplayObjectContainer;	//路径层
		private $blocks: egret.Shape[];						//地块

		constructor(map: AStarMap) {
			this.$map = map;
			this.$blocks = [];
		}

		public get pathLayer(): egret.DisplayObjectContainer {
			if (!this.$pathLayer) {
				this.$pathLayer = ui.setLayer(new egret.DisplayObjectContainer,
					!!this.$grids ? 1 : 2, this.$map, { name: '路径层', touchEnabled: false, visible: false });
			}
			return this.$pathLayer;
		}

		public redrawBlock(node: AStarNode): egret.Shape {
			let index = this._indexOf(node.col, node.row);
			let fillColor = undefined;
			if (!node.walkable) fillColor = AStarViewer.DFT_OBST_CLR;
			else if (node.flag) fillColor = AStarViewer.DFT_FLAG_CLR;
			let fillAlpha = 0;
			if (!node.walkable) fillAlpha = AStarViewer.DFT_OBST_ALF;
			else if (node.flag) fillAlpha = AStarViewer.DFT_FLAG_ALF;
			this.$blocks[index] = ui.drawRect({
				width: this.$map.nodeWidth,
				height: this.$map.nodeHeight,
				centered: true,
				fillColor, fillAlpha
			}, this.$blocks[index]);
			return this.$blocks[index];
		}

		public showGrids(): void {
			this.$grids = ui.setTop(ui.drawGrids({
				colNum: this.$map.colNum,
				rowNum: this.$map.rowNum,
				gridWidth: this.$map.nodeWidth,
				gridHeight: this.$map.nodeHeight,
			}, this.$grids), this.$map, { name: '网格', touchEnabled: false });
			this.$grids.visible = true;
			this.$grids.touchEnabled = false;
		}

		public hideGrids(): void { if (!!this.$grids) this.$grids.visible = false; }

		public showBlocks(): void {
			if (!this.$blocksLayer) {
				this.$blocksLayer = ui.setBottom(new egret.DisplayObjectContainer, this.$map,
					{ name: '地块层', touchEnabled: false });
				ui.resetGraphics().resetThickness(0).resetFillColor(Color.PURPLE);
				for (let row = 0; row < this.$map.rowNum; row++) {
					for (let col = 0; col < this.$map.colNum; col++) {
						let node = this.$map.getNode(col, row);
						let [x, y] = node.vector2;
						ui.setDisplay(this.redrawBlock(node), { x, y }, this.$blocksLayer);
					}
				}
				this.$blocksLayer.touchChildren = false;
				this.$blocksLayer.cacheAsBitmap = true;
				if (this.$map.cache !== false) this.$blocksLayer.cacheAsBitmap = true;
			} else this.$blocksLayer.visible = true;
		}

		public hideBlocks(): void { if (!!this.$blocksLayer) this.$blocksLayer.visible = false; }

		public showPaths(): void {
			this.pathLayer.visible = true;
		}

		public hidePaths(): void { if (!!this.$pathLayer) this.$pathLayer.visible = false; }

		private _indexOf(col: number, row: number): number { return row * this.$map.colNum + col; }

		public blockAt(col: number, row: number): egret.Shape {
			return this.$blocks[this._indexOf(col, row)];
		}
	}
}